/**
 * 
 */
package fr.ece.ing4.chatapplication.client;

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author enzo
 *
 */
public class ClientFrame extends JFrame implements ActionListener, ChangeListener {

    private ClientController clientController;

    private JPanel globalPanel; // Un panel global pour mettre les autres panels dedans
    private JPanel messagePanel; // Un panel pour écrire le message mais on a pas besoin d'un panel forcément, un TextEdit suffira surement
    private JPanel controlPanel;  // Un panel pour afficher les rooms dispo + les boutons genre join/leave create/delete
    private JScrollPane chatRoomPane;


    private JList<String> chatRoomList;
    private DefaultListModel<String> dlm;

    private JButton createBtn, deleteBtn, joinBtn, leaveBtn, sendMessageBtn;
    private JLabel availableChatRoom;
    private JTabbedPane chatRoomTabbedPane;
    private JTextField messageZone;

    private JScrollPane scrollChat1, scrollChat2, scrollChat3;
    private JPanel chatPanel1, chatPanel2, chatPanel3;
    private JScrollPane scrollList1, scrollList2, scrollList3;
    private JTextArea chat1, chat2, chat3;
    private JList<String> usersList1, usersList2, usersList3;
    private DefaultListModel<String> user1, user2, user3;

    private void initComponents () {

        globalPanel = new JPanel(new GridBagLayout());
        messagePanel = new JPanel(new GridBagLayout());
        controlPanel = new JPanel(new GridBagLayout());
        chatRoomTabbedPane = new JTabbedPane();
        messageZone = new JTextField();

        chatPanel1 = new JPanel(new GridBagLayout());
        chatPanel2 = new JPanel(new GridBagLayout());
        chatPanel3 = new JPanel(new GridBagLayout());

        chat1 = new JTextArea();
        chat2 = new JTextArea();
        chat3 = new JTextArea();

        scrollChat1 = new JScrollPane(chat1);
        scrollChat2 = new JScrollPane(chat2);
        scrollChat3 = new JScrollPane(chat3);

        user1 = new DefaultListModel<String>();
        user2 = new DefaultListModel<String>();
        user3 = new DefaultListModel<String>();

        usersList1 = new JList<String>(user1);
        usersList2 = new JList<String>(user2);
        usersList3 = new JList<String>(user3);

        scrollList1 = new JScrollPane(usersList1);
        scrollList2 = new JScrollPane(usersList2);
        scrollList3 = new JScrollPane(usersList3);

        dlm = new DefaultListModel<String>();
        chatRoomList = new JList<String>(dlm);
        chatRoomPane = new JScrollPane(chatRoomList);

        createBtn = new JButton("CREATE CHATROOM");
        deleteBtn = new JButton("DELETE CHATROOM");
        joinBtn = new JButton("JOIN CHATROOM");
        leaveBtn = new JButton("LEAVE CHATROOM");
        sendMessageBtn = new JButton("SEND MESSAGE");
        availableChatRoom = new JLabel("          AVAILABLE CHAT ROOMS");
    }

    private void setComponentsProperties() {

        // SIZE //

        controlPanel.setMinimumSize(new Dimension(200, 600));
        controlPanel.setPreferredSize(new Dimension(200, 600));
        controlPanel.setSize(new Dimension(200, 600));

        chatRoomTabbedPane.setMinimumSize(new Dimension(600, 550));
        chatRoomTabbedPane.setPreferredSize(new Dimension(600, 550));
        chatRoomTabbedPane.setSize(new Dimension(600, 550));

        messageZone.setMinimumSize(new Dimension(450, 50));
        messageZone.setPreferredSize(new Dimension(450, 50));
        messageZone.setSize(new Dimension(450, 50));

        messagePanel.setMinimumSize(new Dimension(600, 50));
        messagePanel.setPreferredSize(new Dimension(600, 50));
        messagePanel.setSize(new Dimension(600, 50));

        chatRoomPane.setMinimumSize(new Dimension(200, 350));
        chatRoomPane.setPreferredSize(new Dimension(200, 350));
        chatRoomPane.setSize(new Dimension(200, 350));

        createBtn.setMinimumSize(new Dimension(200, 55));
        createBtn.setPreferredSize(new Dimension(200, 55));
        createBtn.setSize(new Dimension(200, 55));

        deleteBtn.setMinimumSize(new Dimension(200, 55));
        deleteBtn.setPreferredSize(new Dimension(200, 55));
        deleteBtn.setSize(new Dimension(200, 55));

        leaveBtn.setMinimumSize(new Dimension(200, 55));
        leaveBtn.setPreferredSize(new Dimension(200, 55));
        leaveBtn.setSize(new Dimension(200, 55));

        joinBtn.setMinimumSize(new Dimension(200, 55));
        joinBtn.setPreferredSize(new Dimension(200, 55));
        joinBtn.setSize(new Dimension(200, 55));

        scrollList1.setMinimumSize(new Dimension(200, 550));
        scrollList1.setPreferredSize(new Dimension(200, 550));
        scrollList1.setSize(new Dimension(200, 550));

        scrollList2.setMinimumSize(new Dimension(200, 550));
        scrollList2.setPreferredSize(new Dimension(200, 550));
        scrollList2.setSize(new Dimension(200, 550));

        scrollList3.setMinimumSize(new Dimension(200, 550));
        scrollList3.setPreferredSize(new Dimension(200, 550));
        scrollList3.setSize(new Dimension(200, 550));

        scrollChat1.setMinimumSize(new Dimension(400, 550));
        scrollChat1.setPreferredSize(new Dimension(400, 550));
        scrollChat1.setSize(new Dimension(400, 550));

        scrollChat2.setMinimumSize(new Dimension(400, 550));
        scrollChat2.setPreferredSize(new Dimension(400, 550));
        scrollChat2.setSize(new Dimension(400, 550));

        scrollChat3.setMinimumSize(new Dimension(400, 550));
        scrollChat3.setPreferredSize(new Dimension(400, 550));
        scrollChat3.setSize(new Dimension(400, 550));

        sendMessageBtn.setMinimumSize(new Dimension(150, 50));
        sendMessageBtn.setPreferredSize(new Dimension(150, 50));
        sendMessageBtn.setSize(new Dimension(150, 50));

        availableChatRoom.setMinimumSize(new Dimension(200, 20));
        availableChatRoom.setPreferredSize(new Dimension(200, 20));
        availableChatRoom.setSize(new Dimension(200, 20));

        // BORDERS //

        chatRoomPane.setBorder(new EtchedBorder(EtchedBorder.RAISED));

        // TEXTAREAS //

        chat1.setEditable(false);
        chat2.setEditable(false);
        chat3.setEditable(false);

        chat1.setLineWrap(true);
        chat2.setLineWrap(true);
        chat3.setLineWrap(true);
    }

    private void addComponentsWithConstraints(){

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.CENTER;
        controlPanel.add(availableChatRoom, gbc);
        gbc.gridx = 0; gbc.gridy = 1; gbc.fill = GridBagConstraints.HORIZONTAL;
        controlPanel.add(chatRoomPane, gbc);
        gbc.gridx = 0; gbc.gridy = 2; gbc.fill = GridBagConstraints.HORIZONTAL;
        controlPanel.add(createBtn, gbc);
        gbc.gridx = 0; gbc.gridy = 3; gbc.fill = GridBagConstraints.HORIZONTAL;
        controlPanel.add(deleteBtn, gbc);
        gbc.gridx = 0; gbc.gridy = 4; gbc.fill = GridBagConstraints.HORIZONTAL;
        controlPanel.add(joinBtn, gbc);
        gbc.gridx = 0; gbc.gridy = 5; gbc.fill = GridBagConstraints.HORIZONTAL;
        controlPanel.add(leaveBtn, gbc);

        gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        messagePanel.add(messageZone, gbc);
        gbc.gridx = 1; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        messagePanel.add(sendMessageBtn, gbc);


        gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        chatPanel1.add(scrollChat1, gbc);
        gbc.gridx = 1; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        chatPanel1.add(scrollList1, gbc);

        gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        chatPanel2.add(scrollChat2, gbc);
        gbc.gridx = 1; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        chatPanel2.add(scrollList2, gbc);

        gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        chatPanel3.add(scrollChat3, gbc);
        gbc.gridx = 1; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL;
        chatPanel3.add(scrollList3, gbc);


        gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.gridheight = 4; gbc.fill = GridBagConstraints.HORIZONTAL;
        globalPanel.add(controlPanel, gbc);
        gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 3; gbc.gridheight = 2; gbc.fill = GridBagConstraints.HORIZONTAL;
        globalPanel.add(chatRoomTabbedPane, gbc);
        gbc.gridx = 1; gbc.gridy = 2; gbc.gridwidth = 3; gbc.gridheight = 1; gbc.fill = GridBagConstraints.HORIZONTAL;
        globalPanel.add(messagePanel, gbc);
    }

    private void addComponentsListener() {

        chatRoomTabbedPane.addChangeListener(this);

        createBtn.addActionListener(this);
        deleteBtn.addActionListener(this);

        joinBtn.addActionListener(this);
        leaveBtn.addActionListener(this);

        sendMessageBtn.addActionListener(this);

    }

    public ClientFrame(ClientController clientController) {

        this.clientController = clientController;
        this.setTitle("Chat Application: " + clientController.getClientName());

        initComponents();

        setComponentsProperties();

        addComponentsWithConstraints();

        addComponentsListener();

        //////////////////////////////////////////////////////////////////////
        //                                                                  //
        //    A remplacer par un Listener pour charger les chatrooms !      //
        //                                                                  //
        //////////////////////////////////////////////////////////////////////

        chatRoomTabbedPane.addTab("CHATROOM 1", chatPanel1);
        chatRoomTabbedPane.addTab("CHATROOM 2", chatPanel2);
        chatRoomTabbedPane.addTab("CHATROOM 3", chatPanel3);

        this.add(globalPanel);

        this.setSize(new Dimension(800, 620));
        this.setResizable(false);
        this.addWindowListener(new WindowEventHandler(clientController));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void writeMessage(String data, int chatRoomNumber) {

        if (chatRoomNumber == 0 ) { chat1.append(data + "\n" ); }
        else if (chatRoomNumber == 1 ) { chat2.append(data + "\n" ); }
        else if (chatRoomNumber == 2 ) { chat3.append(data + "\n" ); }

    }

    public void changeTitleTab(String chatRoomName, int index ) {
        chatRoomTabbedPane.setTitleAt(index - 1 , chatRoomName);
    }

    public void addChatroomToList(String chatRoomName) {
        dlm.addElement(chatRoomName);
    }

    public void clearChatroomTab(int index ) {
        if (index == 1 ) {
            chat1.setText("");
            user1.clear();
        } else if (index == 2 ) {
            chat2.setText("");
            user2.clear();
        } else if (index == 3 ) {
            chat3.setText("");
            user3.clear();
        }
    }

    public void deleteChatroomFromList(String chatRoomName) {
        dlm.removeElement(chatRoomName);
    }

    public void deleteAllChatFromList() {
        dlm.clear();
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource().equals(createBtn)) {
            new PopupFrame("Enter Chatroom Name", "Enter the name of the chatroom" , clientController);
        }

        if(e.getSource().equals(joinBtn)) {
            clientController.joinChatRoom(chatRoomList.getSelectedValue());
        }

        if(e.getSource().equals(deleteBtn)) {
            clientController.leaveChatRoom(chatRoomList.getSelectedValue());

            if ( chatRoomTabbedPane.getTitleAt(0).equals(chatRoomList.getSelectedValue()) ) {
                changeTitleTab("CHATROOM 1", 1);
                clearChatroomTab(1);
            } else if ( chatRoomTabbedPane.getTitleAt(1).equals(chatRoomList.getSelectedValue()) ) {
                changeTitleTab("CHATROOM 2", 2);
                clearChatroomTab(2);
            } else if ( chatRoomTabbedPane.getTitleAt(2).equals(chatRoomList.getSelectedValue()) ) {
                changeTitleTab("CHATROOM 3", 3);
                clearChatroomTab(3);
            }
            deleteChatroomFromList(chatRoomList.getSelectedValue());
        }

        if(e.getSource().equals(leaveBtn)) {
            clientController.leaveChatRoom(chatRoomList.getSelectedValue());

            if ( chatRoomTabbedPane.getTitleAt(0).equals(chatRoomList.getSelectedValue()) ) {
                changeTitleTab("CHATROOM 1", 1);
                clearChatroomTab(1);
            } else if ( chatRoomTabbedPane.getTitleAt(1).equals(chatRoomList.getSelectedValue()) ) {
                changeTitleTab("CHATROOM 2", 2);
                clearChatroomTab(2);
            } else if ( chatRoomTabbedPane.getTitleAt(2).equals(chatRoomList.getSelectedValue()) ) {
                changeTitleTab("CHATROOM 3", 3);
                clearChatroomTab(3);
            }
            deleteChatroomFromList(chatRoomList.getSelectedValue());
        }

        if( e.getSource().equals(sendMessageBtn) ) {
            if (messageZone.getText() != "") {

                clientController.sendMessage(chatRoomTabbedPane.getTitleAt(chatRoomTabbedPane.getSelectedIndex()), "<" + clientController.getClientName() + "> : " + messageZone.getText());
                writeMessage(messageZone.getText(), chatRoomTabbedPane.getSelectedIndex());
                messageZone.setText("");
            }
        }
  
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (chatRoomTabbedPane.getSelectedIndex() == 2 ) {


        }
    }

}
